﻿using System;
using System.Collections.Generic;

namespace Com.PhilChuang.Utils
{
	public static class AppServiceLocator
	{
		private static readonly List<Action> s_CachedActions = new List<Action> ();
		private static readonly Object lock_s_CachedActions = new object ();
		private static IServiceLocator s_Instance;
		
		public static IServiceLocator Instance
		{
			get { return s_Instance; }
			set
			{
				s_Instance = value;
				lock (lock_s_CachedActions)
				{
					s_CachedActions.ForEach (a => a ());
				}
			}
		}

		public static void WhenInstanceSet (Action a)
		{
			lock (lock_s_CachedActions)
			{
				if (s_Instance != null)
				{
					a ();
					return;
				}
				s_CachedActions.Add (a);
			}
		}
	}
}